var partNum;
var totalGetPoint=0;
$(document).ready(function(){
	$.ajax({
		url:"../reloadTestInf",
		type:"post",
		data:{
			testid:getQueryString("test_id"),
			partNo:0,
			questionNo:0,
			type:1,
		},
		async: false,
		success:function(res){
			partNum=res.partNum;
			$("#partNum").val(partNum);
			$("#totalPoint").val(res.totalPoint);
			$("#testName").html(res.testName);
		}
	});	
	creatTestInf();
	setPart();
	
});
function getQueryString(name) {
	    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	    var r = window.location.search.substr(1).match(reg);
	    if (r != null) return unescape(r[2]);
	    return null;
}

function creatTestInf(){
	partNum=$("#partNum").val();
	$("#parts").empty();
	for(var i=1;i<=partNum;i++){
		$.ajax({
			url:"htmlPart/parts.html",
			type:"post",
			data:{},
			async: false,
			dataType: "html",
			success:function(res){
				$("#parts").append($(res));
				
				$("#partNo").html("第"+i+"部分");
				$("#partNo").attr("id","partNo"+i);
				$("#partNo"+i).attr("name",i);
				
				$("#partName").attr("id","partName"+i);
				$("#typeList").attr("id","typeList"+i);
				$("#questionNum").attr("id","questionNum"+i);
				$("#perQuestionPoint").attr("id","perQuestionPoint"+i);
				
				$("#questions").attr("name",i);
				$("#questions").attr("id","questions"+i);			
				
			}
		});	
	}
};

function setPart(){
	partNum=$("#partNum").val();
	$("#testName").attr("disabled","disabled");
	$("#totalPoint").attr("disabled","disabled");
	$("#partNum").attr("disabled","disabled");

	for(var i=1;i<=partNum;i++){
		$.ajax({
			url:"../reloadTestInf",
			type:"post",
			data:{
				testid:getQueryString("test_id"),
				partNo:i,
				questionNo:0,
				type:2,
			},
			async: false,
			success:function(res){
				$("#partName"+i).val(res.partName);
				$("#typeList"+i).val(res.typeList);
				$("#questionNum"+i).val(res.questionNum);
				$("#perQuestionPoint"+i).val(res.perPoint);
			}
		});
		$("#partName"+i).attr("disabled","disabled");
		$("#typeList"+i).attr("disabled","disabled");
		$("#questionNum"+i).attr("disabled","disabled");
		$("#perQuestionPoint"+i).attr("disabled","disabled");
		
		addTopic(i,$("#questionNum"+i).val());
		
	}  
	
}

function addTopic(partNo,questionNum){
	if($("#typeList"+partNo).val()==1){
		for(var i=1;i<=questionNum;i++){
			addSingle(partNo,i);
		}	
	}else if($("#typeList"+partNo).val()==2){
		for(var i=1;i<=questionNum;i++){
			addMultiple(partNo,i);
		}
	}else if($("#typeList"+partNo).val()==3){
		for(var i=1;i<=questionNum;i++){
			addSubject(partNo,i);
		}
	}
	
}

function addSingle(partNo,questionNo){
	$.ajax({
		url:"htmlPart/single.html",
		type:"get",
		data:{},
		dataType: "html",
		success:function(res){
			$("#questions"+partNo).append($(res));
			
			$("#searchQuestion").remove();
			$("#cancel").remove();
			$("#preserve").remove();
			
			var body=document.getElementById("new");
			var children=body.childNodes;
			children[1].innerHTML="第"+questionNo+"题 单选";
			
			$("#new").addClass("1");
			$("#new").attr("id",partNo+"topic"+questionNo);
			
			$("#question").attr("disabled","disabled");
			$("#question").attr("id",partNo+"topic"+questionNo+"q");
			
			$("#A").attr("disabled","disabled");
			$("#B").attr("disabled","disabled");
			$("#C").attr("disabled","disabled");
			$("#D").attr("disabled","disabled");
			$("#A").attr("id",partNo+"topic"+questionNo+"A");
			$("#B").attr("id",partNo+"topic"+questionNo+"B");
			$("#C").attr("id",partNo+"topic"+questionNo+"C");
			$("#D").attr("id",partNo+"topic"+questionNo+"D");
			
			$("#setSubject").attr("disabled","disabled");
			$("#setKnowledgePoint").attr("disabled","disabled");
			$("#setSubject").attr("id",partNo+"topic"+questionNo+"setSubject");
			$("#setKnowledgePoint").attr("id",partNo+"topic"+questionNo+"setKnowledgePoint");
			
			$("#answerPart").attr("id",partNo+"topic"+questionNo+"answerPart");
			
			var checks=document.getElementsByName("checks");
			for(var i=0;i<4;i++){
				checks[0].setAttribute("name",partNo+"topic"+questionNo+"answer");
			}
			
			$.ajax({
				url:"../reloadTestInf",
				type:"post",
				data:{
					testid:getQueryString("test_id"),
					partNo:partNo,
					questionNo:questionNo,
					type:3,
				},
				async: false,
				success:function(res){
		        	loadSearchResult(partNo,questionNo,res.questionId,1)
				}
			});
		}
	});		
}

function addMultiple(partNo,questionNo){
	$.ajax({
		url:"htmlPart/multiple.html",
		type:"get",
		data:{},
		dataType: "html",
		success:function(res){
			$("#questions"+partNo).append($(res));
			
			$("#searchQuestion").remove();
			$("#cancel").remove();
			$("#preserve").remove();
			
			var body=document.getElementById("new");
			var children=body.childNodes;
			children[1].innerHTML="第"+questionNo+"题 多选";
			
			$("#new").addClass("2");
			$("#new").attr("id",partNo+"topic"+questionNo);
			
			$("#question").attr("disabled","disabled");
            $("#question").attr("id",partNo+"topic"+questionNo+"q");

            $("#A").attr("disabled","disabled");
			$("#B").attr("disabled","disabled");
			$("#C").attr("disabled","disabled");
			$("#D").attr("disabled","disabled");
            $("#A").attr("id",partNo+"topic"+questionNo+"A");
			$("#B").attr("id",partNo+"topic"+questionNo+"B");
			$("#C").attr("id",partNo+"topic"+questionNo+"C");
			$("#D").attr("id",partNo+"topic"+questionNo+"D");
			
			$("#setSubject").attr("disabled","disabled");
			$("#setKnowledgePoint").attr("disabled","disabled");
			$("#setSubject").attr("id",partNo+"topic"+questionNo+"setSubject");
			$("#setKnowledgePoint").attr("id",partNo+"topic"+questionNo+"setKnowledgePoint");
			
			$("#answerPart").attr("id",partNo+"topic"+questionNo+"answerPart");
			
			var checks=document.getElementsByName("checks");
			for(var i=0;i<4;i++){
				checks[0].setAttribute("name",partNo+"topic"+questionNo+"answer");
			}
			
			$.ajax({
				url:"../reloadTestInf",
				type:"post",
				data:{
					testid:getQueryString("test_id"),
					partNo:partNo,
					questionNo:questionNo,
					type:3,
				},
				async: false,
				success:function(res){
		        	loadSearchResult(partNo,questionNo,res.questionId,2)
				}
			});
		
		}
	});		
};
function addSubject(partNo,questionNo){
	$.ajax({
		url:"htmlPart/subject.html",
		type:"get",
		data:{},
		dataType: "html",
		success:function(res){
			$("#questions"+partNo).append($(res));
			
			$("#searchQuestion").remove();
			$("#cancel").remove();
			$("#preserve").remove();
			
			var body=document.getElementById("new");
			var children=body.childNodes;
			children[1].innerHTML="第"+questionNo+"题 主观题";
			
			$("#new").addClass("3");
			$("#new").attr("id",partNo+"topic"+questionNo);
			
			$("#question").attr("disabled","disabled");
			$("#question").attr("id",partNo+"topic"+questionNo+"q");
			$("#answer").attr("disabled","disabled")
			$("#answer").attr("id",partNo+"topic"+questionNo+"a");
			
			$("#setSubject").attr("disabled","disabled");
			$("#setKnowledgePoint").attr("disabled","disabled");
			$("#setSubject").attr("id",partNo+"topic"+questionNo+"setSubject");
			$("#setKnowledgePoint").attr("id",partNo+"topic"+questionNo+"setKnowledgePoint");
			
			$("#answerPart").attr("id",partNo+"topic"+questionNo+"answerPart");
			
			$.ajax({
				url:"../reloadTestInf",
				type:"post",
				data:{
					testid:getQueryString("test_id"),
					partNo:partNo,
					questionNo:questionNo,
					type:3,
				},
				async: false,
				success:function(res){
		        	loadSearchResult(partNo,questionNo,res.questionId,3)
				}
			});
			
		}
	});		
}

function loadSearchResult(partNo,questionNo,questionId,type){
	$.ajax({
		url:"../loadQuestionInf",
		type:"post",
		data:{
			questionId:questionId,
			type:type,
		},
		async: false,
		success:function(res){		
			if(type==1){
				$("#"+partNo+"topic"+questionNo+"q").val(res.topic);
				$("#"+partNo+"topic"+questionNo+"A").val(res.A);
				$("#"+partNo+"topic"+questionNo+"B").val(res.B);
				$("#"+partNo+"topic"+questionNo+"C").val(res.C);
				$("#"+partNo+"topic"+questionNo+"D").val(res.D);
				
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='A']").attr("disabled","disabled");
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='B']").attr("disabled","disabled");
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='C']").attr("disabled","disabled");
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='D']").attr("disabled","disabled");
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='"+res.answer+"']").removeAttr("disabled");
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='"+res.answer+"']").attr("checked",true);
				
				$("#"+partNo+"topic"+questionNo+"setSubject").val(res.subject);
				
				$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val(res.knowledgePoint);
				
				loadPointCondition(partNo,questionNo,type);
				
			}else if(type==2){
				$("#"+partNo+"topic"+questionNo+"q").val(res.topic);
				$("#"+partNo+"topic"+questionNo+"A").val(res.A);
				$("#"+partNo+"topic"+questionNo+"B").val(res.B);
				$("#"+partNo+"topic"+questionNo+"C").val(res.C);
				$("#"+partNo+"topic"+questionNo+"D").val(res.D);
				
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='A']").attr("checked",false);
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='B']").attr("checked",false);
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='C']").attr("checked",false);
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='D']").attr("checked",false);

				if(res.answer_A!="null"){
					$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='A']").attr("checked",true);
				}else {$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='A']").attr("disabled","disabled");}
				if(res.answer_B!="null"){
					$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='B']").attr("checked",true);
				}else {$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='B']").attr("disabled","disabled");}
				if(res.answer_C!="null"){
					$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='C']").attr("checked",true);
				}else {$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='C']").attr("disabled","disabled");}
				if(res.answer_D!="null"){
					$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='D']").attr("checked",true);
				}else {$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='D']").attr("disabled","disabled");}
				
				$("#"+partNo+"topic"+questionNo+"setSubject").val(res.subject);
				
				$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val(res.knowledgePoint);
				
				loadPointCondition(partNo,questionNo,type)
				
			}else if(type==3){
				$("#"+partNo+"topic"+questionNo+"q").val(res.topic);
				$("#"+partNo+"topic"+questionNo+"a").val(res.answer);
				
                $("#"+partNo+"topic"+questionNo+"setSubject").val(res.subject);
				
				$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val(res.knowledgePoint);
				
				loadPointCondition(partNo,questionNo,type);
				
			}
		}
	});
}

function loadPointCondition(partNo,questionNo,type){
	$.ajax({
		url:"../getStudentTestInf",
		type:"post",
		data:{
			partNo:partNo,
			questionNo:questionNo,
			type:type,
		},
		async: false,
		success:function(res){
			if(res.code!=3){
				$.ajax({
					url:"htmlPart/answerPart.html",
					type:"post",
					data:{},
					async: false,
					success:function(code){	
						$("#"+partNo+"topic"+questionNo+"answerPart").append(code);
						$("#yourAnswer").attr("id",partNo+"topic"+questionNo+"yourAnswer");
						$("#"+partNo+"topic"+questionNo+"yourAnswer").html(res.answer);
						$("#yourPoint").attr("id",partNo+"topic"+questionNo+"yourPoint");
						$("#"+partNo+"topic"+questionNo+"yourPoint").html(res.grade);
						totalGetPoint+=parseInt(res.grade);	
						$("#totalGetPoint").empty();
						$("#totalGetPoint").append("<h3>总得分："+totalGetPoint+"</h3>");
					}
				});
			}else {
				$.ajax({
					url:"htmlPart/answerPart.html",
					type:"post",
					data:{},
					async: false,
					success:function(code){	
						$("#"+partNo+"topic"+questionNo+"answerPart").append(code);
						$("#yourAnswer").attr("id",partNo+"topic"+questionNo+"yourAnswer");
						$("#"+partNo+"topic"+questionNo+"yourAnswer").html(res.answer);
						$("#yourPoint").attr("id",partNo+"topic"+questionNo+"yourPoint");
						$("#"+partNo+"topic"+questionNo+"yourPoint").html("等待老师批阅");	
						$("#totalGetPoint").empty();
						$("#totalGetPoint").append("<h3>总得分："+totalGetPoint+"</h3>");
					}
				});
			}
			
		}
	});
}